﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Odds.Entity;
using Odds.DataAccess;
using System.Text;

public partial class Page_CorrectRate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int lastIssueId = new OddsIssueController().GetLastIssueRealNumber() - 1;

        StringBuilder outputBegin = new StringBuilder();
        StringBuilder outputEnd = new StringBuilder();

        for (int i = 0; i < 10; i++)
        {
            int issueRealId = lastIssueId - i;
            Dictionary<string, MatchResultRate> dicRateBegin = GetCorrectRate(issueRealId, 1);
            Dictionary<string, MatchResultRate> dicRateEnd = GetCorrectRate(issueRealId, 2);
            outputBegin.Append(Output(issueRealId, dicRateBegin));
            outputEnd.Append(Output(issueRealId, dicRateEnd));
        }

        divBegin.InnerHtml = outputBegin.ToString();
        divEnd.InnerHtml = outputEnd.ToString();
    }

    private string Output(int issueRealId, Dictionary<string, MatchResultRate> dicRate)
    {
        StringBuilder result = new StringBuilder();
        Dictionary<string, MatchResultRate> dicOrder =
            dicRate.OrderByDescending(o => o.Value.RightCount / o.Value.TotalCount).ToDictionary(t => t.Key, t => t.Value);
        result.Append("<table style='width:300px; border:1px; border-style:solid;'>");
        result.AppendFormat("<tr><td>Issue</td><td>Company</td><td>Count</td><td>Rate</td></tr>");
        result.AppendFormat("<tr><td rowspan='{0}'>{1}</td></tr>", dicRate.Count + 1, issueRealId);
        foreach (KeyValuePair<string, MatchResultRate> item in dicOrder)
        {
            result.Append("<tr>");
            result.AppendFormat("<td>{0}</td><td>{1}/{2}</td><td>{3}</td>",
                item.Key, item.Value.RightCount, item.Value.TotalCount, item.Value.RightRate);
            result.Append("</tr>");
        }
        result.Append("</table><br />");

        return result.ToString();
    }

    private Dictionary<string, MatchResultRate> GetCorrectRate(int issueRealId, int beginOrEnd)
    {
        Dictionary<string, MatchResultRate> result = new Dictionary<string,MatchResultRate>();
        List<OddsBeginOrEndCount> datasource =
            new OddsSPController().GetIssueOddsBeginOrEndRecordCountByIssueIDWithAdjust(issueRealId.ToString(), 0, 1);
        List<OddsIssueItem> resultSet =
            new OddsIssueItemController().GetByIssueRealId(issueRealId);

        foreach (OddsBeginOrEndCount oddsBeginOrEndCount in datasource)
        {
            int matchResult =
                resultSet.Where(s => s.IndexInIssue == oddsBeginOrEndCount.IndexInIssue).First().MatchResult;

            float winCount = 0;
            float drawCount = 0;
            float loseCount = 0;

            if (beginOrEnd == 1)
            {
                winCount = oddsBeginOrEndCount.WinCountBegin;
                drawCount = oddsBeginOrEndCount.DrawCountBegin;
                loseCount = oddsBeginOrEndCount.LoseCountBegin;
            }
            else
            {
                winCount = oddsBeginOrEndCount.WinCountEnd;
                drawCount = oddsBeginOrEndCount.DrawCountEnd;
                loseCount = oddsBeginOrEndCount.LoseCountEnd;
            }
            float totalCount = winCount + drawCount + loseCount;

            float rate = 0;
            if (matchResult == 3)
            {
                rate = winCount / totalCount;
            }
            else if (matchResult == 1)
            {
                rate = drawCount / totalCount;
            }
            else if (matchResult == 0)
            {
                rate = loseCount / totalCount;
            }

            if (!result.ContainsKey(oddsBeginOrEndCount.CompanyName))
            {
                result.Add(oddsBeginOrEndCount.CompanyName, new MatchResultRate());
            }

            result[oddsBeginOrEndCount.CompanyName].RightRate += rate;

            if (rate > 0.5)
            {
                result[oddsBeginOrEndCount.CompanyName].RightCount++;
            }
        }

        foreach (KeyValuePair<string, MatchResultRate> item in result)
        {
            item.Value.TotalCount = datasource.Count(c => c.CompanyName == item.Key);
        }

        return result;
    }

    private class MatchResultRate
    {
        public float TotalCount
        {
            get;
            set;
        }

        public float RightCount
        {
            get;
            set;
        }

        public float RightRate
        {
            get;
            set;
        }
    }
}